package com.blog.controller;

import com.alibaba.druid.util.StringUtils;
import com.blog.pojo.Admin;
import com.blog.pojo.Blogusers;
import com.blog.pojo.PageResult;
import com.blog.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: VE
 * @DATE: 2022/10/11 11:56
 * 描述:
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    private Logger logger = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    private AdminService adminService;

    @GetMapping("test1")
    public String test1(){
        return "1111";
    }

    @GetMapping("login")
    public PageResult login(String adminName, String adminPass, HttpServletResponse rep){
        logger.info("管理员登录"+adminName+"================="+adminPass);
        if(adminName==null || StringUtils.isEmpty(adminName)){
            return PageResult.none("不能为空");
        }
        Admin loginUser = adminService.login(adminName);
        if(loginUser==null|| !loginUser.getAdminPass().equals(adminPass)){
            return PageResult.none("用户名或者密码错误");
        }
        Cookie cookie = new Cookie("loginUser_id",loginUser.getAdminId()+"");
        cookie.setMaxAge(3600);
        cookie.setPath("/");
        rep.addCookie(cookie);
        return PageResult.success("登录成功",loginUser);
    }
}
